*Data sets for the graphs

*** BE CAREFUL:
* HERE WE MODIFIED LEVEL 5 OF ADD VARIANCE OF TASKS, LEVEL 3 AND 6 MULT W/O SCALE INVARIANCE


* Fine Motor
* With scale invariance 
global path "Y:\Fuyao_Erlfang\estimates\maturation"
use "$path/fine_add/bootsresults.dta", clear 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=(var8) // Remove 1.4
foreach num of numlist 9/14{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	 // Remove 1.4	
}
foreach num of numlist 15/20 {
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 27/31{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var20_2 var21-var26 var27_2-var31_2 var32 var33{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}


keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 21/26 32/33{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/20 27/31{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q

rename numero n
merge m:1 n using "$path/fine_add/point_estimates_add_mat.dta"
drop _merge
drop additive 
rename m1 add_pe
keep if q==1
gen var=""
replace var ="Kbar" if n>7 & n<15
replace var=" $\sigma_{s,\ell}^{2}$" if n<21 & var==""
replace var="Delta" if var==""
drop if n>20 & n<27
drop if n<8
drop if n>31
drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n var var1 level add_pe


/*** Be careful drop this when we have final results!!!
 replace var_ub_ = add_pe + 1.96*var_sd_ if n==18
 replace var_lb_ = add_pe - 1.96*var_sd_ if n==18
*/

*saveold "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/Graphs/Kbar/f/f_add.dta" , replace
saveold "$path/Graphs/f/f_add.dta" , replace



* Multiplicative
use "$path/fine_mul/bootsresults.dta", clear 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8)  // Remove 1.4
foreach num of numlist 9/13{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')		 // Remove 1.4
}
foreach num of numlist 14/19 26{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 27/31{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var19_2 var20-var25 var26_2-var31_2 var32 var33{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}


keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 20/25 32/33{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/19 26/31{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/fine_mul/point_estimates_mul_mat.dta"

drop _merge
drop multiplicative 
rename m2 mul_pe
keep if q==1
cap drop var
gen var=""
replace var ="Kbar" if n>7 & n<14
replace var=" $\sigma_{s,\ell}^{2}$" if n<20 & var==""
replace var="Delta" if var==""
drop if n>19 & n<27
drop if n<8
drop if n>31
drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n var var1 level mul_pe

saveold "$path/Graphs/f/f_mul.dta" , replace


* WithOut scale invariance 
*Additive
global path "Y:\Fuyao_Erlfang\estimates\maturation"
*global path "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/finemotor/fine_add"

 use "$path/fine_level_add/bootsresults.dta", clear 
 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=(var8) //Eliminates 1.4
foreach num of numlist 9/14{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//Eliminates 1.4	
}
foreach num of numlist 15/20 {
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 27/31{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var20_2 var21-var26 var27_2-var31_2 var32-var40{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}


keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 21/26 32/40{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/20 27/31{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/fine_level_add/point_estimates_add_mat_wo.dta"
drop _merge
drop additive 
rename m1 add_pe
keep if q==1
gen var=""
replace var ="Kbar" if n>7 & n<15
replace var=" $\sigma_{s,\ell}^{2}$" if n<21 & var==""
replace var="Delta" if  n>26 & n<32 & var==""
replace var="Beta1" if  n>31 & n<39 & var==""

drop if n>20 & n<27
drop if n<8
drop if n>38
drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n var var1 level add_pe
*Last update: 09/22
saveold "$path/Graphs/f/f_add_wo_si.dta" , replace

* Multiplicative
use "$path/fine_level_mul/bootsresults.dta", clear 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) //Eliminates 1.4
foreach num of numlist 9/13{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//Eliminates 1.4	
}
foreach num of numlist 14/19 26{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 27/31{
gen var`num'_2=((1.4^var`num')/((1.4^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var19_2 var20-var25 var26_2-var31_2 var32-var40{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}
* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var17_2 var18_2 {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}

foreach var of varlist var19_2 {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<993
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}

keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 20/25 32/40{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/19 26/31{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "$path/fine_level_mul/point_estimates_mul_mat_wo.dta"
drop _merge
drop multiplicative
rename  m2 mul_pe
keep if q==1
cap drop var
gen var=""
replace var ="Kbar" if n>7 & n<14
replace var=" $\sigma_{s,\ell}^{2}$" if n<20 & var==""
replace var="Delta" if n>26 & n<32 & var==""
replace var="Beta1" if n>31 & n<39 & var==""
drop if n>19 & n<27
drop if n<8
drop if n>38

drop q
gen level=substr(var1,-2,.)
destring level, replace
order  n  var var1 level mul_pe
/*** Be careful drop this when we have final results!!!
foreach i of numlist 3 /13{
 replace var_ub_ = mul_pe + 1.96*var_sd_ if n==`i'
 replace var_lb_ = mul_pe - 1.96*var_sd_ if n==`i'	
	
}
*/
* Last updated:09/22
saveold "$path/Graphs/f/f_mul_wo_si.dta" , replace


